import {regUserData,doLoginData,safeUserData,outLoginData} from "../../../api/user";
export default {
  namespaced:true,
  state:{
    nickname:localStorage['nickname']?localStorage['nickname']:"",
    uid:localStorage['uid']?localStorage['uid']:"",
    isLogin:localStorage['isLogin']?Boolean(localStorage['isLogin']):"",
    authToken:localStorage['authToken']?localStorage['authToken']:""
  },
  mutations:{
    //设置登录会员信息
    ["SET_USER_LOGIN"](state,payload){
      state.nickname=payload.nickname;
      state.uid=payload.uid;
      state.isLogin=payload.isLogin;
      state.authToken=payload.authToken;
      localStorage['nickname']=state.nickname;
      localStorage['uid']=state.uid;
      localStorage['isLogin']=true;
      localStorage['authToken']=state.authToken;
    },
    //清除会员登录信息
    ["CLEAR_USER_LOGIN"](state,payload){
      state.nickname="";
      state.uid="";
      state.isLogin=false;
      state.authToken="";
      localStorage.clear();
    }
  },
  actions:{
    //注册会员
    regUser(conText,payload){
      regUserData(payload).then(res=>{
        if(payload.complete){
          payload.complete(res);
        }
      })
    },
    //会员登录
    doLogin(conText,payload){
      return doLoginData(payload).then(res=>res);
    },
    //会员token验证
    safeUser(conText,payload){
      safeUserData({uid:conText.state.uid,auth_token:conText.state.authToken}).then(res=>{
        if(payload.complete){
          payload.complete(res)
        }
      })
    },
    //安全退出
    outLogin({state,commit},payload){
      return outLoginData({uid:state.uid}).then(res=>{
        if(res.code==200){
          commit("CLEAR_USER_LOGIN");
        }
        return res;
      })
    }
  }
}
